package com.twu.biblioteca.model;

import com.twu.biblioteca.model.Book;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mvargas on 4/29/14.
 */
public class BookCollection {

    private List<Book> books = new ArrayList<Book>();

    public BookCollection(){
        Book book_1 = new Book(1L, "Dexter: Darkly Dreaming Dexter", "Jeff Lindsay", 6);
        Book book_2 = new Book(2L, "Dexter: Dearly Devoted Dexter", "Jeff Lindsay", 1);
        Book book_3 = new Book(3L, "Dexter: Dexter in the Dark", "Jeff Lindsay", 0);

        books.add(book_1);
        books.add(book_2);
        books.add(book_3);
    }

    public BookCollection(List<Book> books) {
        this.books = books;
    }

    public List<Book> getBooks() {
        return this.books;
    }


    public Book getBookById(Long bookId) {
        for (Book book : this.books){
            if (book.getId().equals(bookId)){
                return book;
            }
        }
        throw new IllegalArgumentException("Sorry we don't have that book yet.");
    }

    public boolean reserveBook(Long idBook) {
        Book book = getBookById(idBook);

        if (book.hasAvailability()){
            book.decreaseQuantityBookAvailable();
            return true;
        }else{
            throw new IllegalArgumentException("Sorry we don't have that book yet.");
        }
    }
}
